<template>
  <div class="zhyhbox">
    <div class="tttop">
      <div class="con-t">User Mgmt</div>
      <div class="con-b">
        <div class="l">
          <div class="item">
            <div class="lab">Name:</div>
            <el-input v-model="username" placeholder="请输入关键词" />
          </div>
          <div class="item">
            <div class="lab">Phone:</div>
            <el-input v-model="phone" placeholder="请输入手机号码" />
          </div>
        </div>
        <div class="r">
          <div class="bu" @click="s_seach">Seach</div>
        </div>
      </div>
    </div>
    <div class="conbox">
      <div class="table">
        <el-table border :data="userdata">
          <el-table-column
            label="索引"
            width="100"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column label="名称" width="400">
            <template #="{ row }">
              <div class="namabox">
                <div class="l">
                  <img v-if="row.img.includes('http')" :src="row.img" alt="" />
                  <img v-else :src="'http://127.0.0.1:3006' + row.img" alt="" />
                </div>
                <div class="r">{{ row.name }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="phone"></el-table-column>
          <el-table-column label="创建时间" prop="createdAt"></el-table-column>
          <el-table-column label="操作" width="100" align="center">
            <template #="{ row, $index }">
              <el-popconfirm title="确定要删除吗?" @confirm="sdelete(row)">
                <template #reference>
                  <el-button type="danger" class="bu">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="fpage">
        <el-pagination
          :default-page-size="10"
          background
          layout="prev, pager, next"
          :total="count"
          @change="s_currpage"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getuser, deleuser } from "@/api/index";
import jlfn from "@/utile/jl";
import { ElNotification } from "element-plus";
const username = ref<string>("");
const phone = ref<string>("");
const limit = ref<number>(10);
const offset = ref<number>(1);
const userdata = ref();
const allpage = ref<number>();
const count = ref<number>();
onMounted(() => {
  getuserdata();
});

const getuserdata = async () => {
  const rudata = await getuser(
    limit.value,
    offset.value,
    username.value,
    phone.value
  );
  if (rudata.code == 200) {
    count.value = rudata.data.count;
    allpage.value = rudata.data.allpage;
    userdata.value = rudata.data.rows;
  }
  console.log(rudata);
};
const fn = jlfn(getuserdata, 1000);
const s_seach = () => {
  fn();
};
const sdelete = async (e: any) => {
  const rudata = await deleuser(e.id);
  if (rudata.code == 200) {
    ElNotification({
      type: "success",
      message: "删除成功！",
      title: "提示",
    });
    getuserdata();
  }
};
const s_currpage = (e: any) => {
  offset.value = e;
  getuserdata();
};
</script>

<style scoped lang="scss">
.bu {
  font-size: 0.6vw;
  cursor: pointer;
}
.zhyhbox {
  height: 100%;
  width: 100%;
  padding: 1vw;
  .conbox {
    height: calc(100% - 170px);
    margin-top: 20px;
    border-radius: 5px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .table {
      height: 80%;
      overflow: hidden;
      border: 1px solid #4c4d4f;
      border-radius: 5px;
      .el-table {
        height: 100%;
        font-size: 0.7vw;
      }
    }
    .namabox {
      height: 100%;
      width: 100%;
      display: flex;
      align-items: center;
      gap: 1vw;
      img {
        height: 2.5vw;
        width: 2.5vw;
        border-radius: 50%;
      }
    }
    .fpage {
      height: 20%;
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      .el-pagination {
      }
    }
  }
  .tttop {
    height: 150px;
    width: 100%;
    background-color: #252931;
    border-radius: 5px;
    border: 1px solid #4c4d4f;
    .con-t {
      height: 30%;
      display: flex;
      align-items: center;
      font-size: 1vw;
      padding: 0 20px;
      color: #fff;
      font-weight: 700;
    }
    .con-b {
      height: 70%;
      display: flex;
      gap: 100px;
      padding: 0 20px;
      .l {
        height: 100%;
        display: flex;
        gap: 3vw;

        .item {
          height: 100%;
          display: flex;
          align-items: center;
          gap: 2vw;
          .el-input {
            font-size: 0.7vw !important;
            height: 2vw;
          }
          .lab {
            color: #fff;
            font-size: 0.8vw;
          }
        }
      }
      .r {
        height: 100%;
        display: flex;
        align-items: center;
        .bu {
          height: 2vw;
          width: 4vw;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 0.75vw;
          color: var(--them-color);
          border: 1px solid var(--them-color);
          border-radius: 5px;
          &:hover {
            transform: scale(1.01);
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>
